package com.team.childapp.http.base;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.convert.StringConvert;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.lzy.okrx2.adapter.ObservableResponse;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Create on: 9/14/21
 * by Mr.Zhu
 * Description: 网络请求BASE基类
 */
public class BaseLogical {

    /**
     * get请求获取数据
     *
     * @param url
     */
    protected void requestStringForGet(String url, Object obj, Observer<Response<String>> observer) {
        OkGo.<String>get(initGetUrl(url, obj))
                .converter(new StringConvert())
                .adapt(new ObservableResponse<String>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * post请求
     * @param url
     * @param obj
     * @param observer
     */
    protected void requestStringForPost(String url, Object obj, Observer<Response<String>> observer) {
        Map<String, String> map = (Map<String, String>) obj;
        //添加公共参数....source=android&appVersion=100&
        Map<Object,Object> params = new HashMap<>();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            params.put(entry.getKey(), entry.getValue());
        }

        OkGo.<String>post(url)
                .converter(new StringConvert())
                .upJson(new Gson().toJson(params))
                .adapt(new ObservableResponse<String>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * post请求
     * from请求
     * @param url
     * @param obj
     * @param observer
     */
    protected void requestStringForPostParam(String url, Object obj, Observer<Response<String>> observer) {
        Map<String, String> map = (Map<String, String>) obj;
        //添加公共参数....source=android&appVersion=100&
        HttpParams params = new HttpParams();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            params.put(entry.getKey(), entry.getValue());
        }

        OkGo.<String>post(url)
                .converter(new StringConvert())
                .params(params)
//                .upJson(new Gson().toJson(params))
                .adapt(new ObservableResponse<String>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }


    /**
     * post 请求
     * content type   json
     * @param url
     * @param obj
     * @param observer
     */
    protected void requestStringForPostNew(String url, Object obj, Observer<Response<String>> observer) {
        Map<String, String> map = (Map<String, String>) obj;
        //添加公共参数....source=android&appVersion=100&
        Map<Object,Object> params = new HashMap<>();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            params.put(entry.getKey(), entry.getValue());
        }
        MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        RequestBody body = RequestBody.create(JSON, new Gson().toJson(params));
        OkGo.<String>post(url)
                .converter(new StringConvert())
                .upRequestBody(body)
                .adapt(new ObservableResponse<String>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }


    /**
     * 文件上传
     * @param url
     * @param params
     * @param observer
     */
    protected void requestFilePost(String url, List<File> params, Observer<Response<String>> observer) {
//        HttpParams param = new HttpParams();
//        param.put("number", params.size());
        OkGo.<String>post(url)
                .converter(new StringConvert())
                .addFileParams("files",params)
//                .params("bucket", Api.IMAGE.BUCKET_NAME)
                .params("bizId","")
                .params("isPress","1")
                .isMultipart(true)
                .adapt(new ObservableResponse<String>())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }


    protected String initGetUrl(String url, Object obj) {
        Map<String, String> map = (Map<String, String>) obj;
        String urlNew = "";
        StringBuilder stringBuilder = new StringBuilder();//创建一个stringBuilder...字符串缓冲区

        stringBuilder.append(url);

        if (url.contains("?")) {
            //?在最后面....2类型
            if (url.indexOf("?") == url.length() - 1) {

            } else {
                //3类型...拼接上&
                stringBuilder.append("&");
            }
        } else {
            //不包含? 属于1类型,,,先拼接上?号
            stringBuilder.append("?");
        }

        //添加公共参数....source=android&appVersion=100&
        for (Map.Entry<String, String> entry : map.entrySet()) {
            //拼接
            stringBuilder.append(entry.getKey())
                    .append("=")
                    .append(entry.getValue())
                    .append("&");
        }

        //删掉最后一个&符号
        if (stringBuilder.indexOf("&") != -1) {
            stringBuilder.deleteCharAt(stringBuilder.lastIndexOf("&"));
        }
        //得到含有公共参数的新路径.....使用新路径去请求
        urlNew = stringBuilder.toString();
        return urlNew;
    }
}
